package com.example.pda.common;

import android.annotation.SuppressLint;
import android.content.Context;

import com.example.pda.data.model.GoodItem;
import com.example.pda.db.DaoMaster;
import com.example.pda.db.DaoSession;
import com.example.pda.db.GoodItemDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class DaoUtils {

    @SuppressLint("StaticFieldLeak")
    static DaoUtils sUtils;

    private static final String DB_NAME = "stock2.db";

    private Context mContext;
    private DaoMaster sDaoMaster;
    private DaoSession sDaoSession;

    public static DaoUtils getsUtils() {
        if (sUtils == null) {
            sUtils = new DaoUtils();
        }
        return sUtils;
    }

    /**
     * 初始化GreenDao,直接在Application中进行初始化操作
     */
    public void initGreenDao(Context context) {
        mContext = context;
        sDaoSession = getDaoSession();
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;

    }

    /**
     * 完成对数据库的添加、删除、修改、查询操作，仅仅是一个接口
     */
    private DaoSession getDaoSession() {
        if (sDaoSession == null) {
            // 判断是否有存在数据库，如果没有则创建
            if (sDaoMaster == null) {
                DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mContext, DB_NAME, null);
                sDaoMaster = new DaoMaster(helper.getWritableDatabase());
            }
            sDaoSession = sDaoMaster.newSession();
        }
        return sDaoSession;
    }
/*
    public GoodItem getDataListByType(Inventory inventory, GoodItem dataList) {
        sDaoSession.getDataListDao().detachAll();
        List<DataList> list = null;
        if (inventory.getBillType().equals("23")) {
            list = sDaoSession.getDataListDao().queryBuilder()
                    .where(DataListDao.Properties.Djh.eq(inventory.getDjh()),
                            DataListDao.Properties.Gysname.eq(dataList.getGysname()),
                            DataListDao.Properties.Invcode.eq(dataList.getInvcode()))
                    .list();
            if (Utils.isEmpty(list)) {
                Log.d("--wyj--", "list is null");
            }
        } else if (inventory.getBillType().equals("4455") || inventory.getBillType().equals("5X")) {
            list = sDaoSession.getDataListDao().queryBuilder()
                    .where(DataListDao.Properties.Gysname.eq(dataList.getGysname()),
                            DataListDao.Properties.Djh.eq(inventory.getDjh()),
                            DataListDao.Properties.Gyspch.eq(dataList.getGyspch()),
                            DataListDao.Properties.Invcode.eq(dataList.getInvcode()))
                    .build()
                    .list();
        }

        if (!Utils.isEmpty(list)) {
            dataList.setPch(list.get(0).getPch());
            dataList.setHgsl(list.get(0).getHgsl());
            dataList.setBz("");
            dataList.setGyscode(list.get(0).getGyscode());
            return dataList;
        }
        return null;
    }*/

    public void insertDataList(GoodItem dataList, int type) {
        if (dataList.getId() != null) {
            dataList.setType(type);
            sDaoSession.getGoodItemDao().update(dataList);
        } else {
            dataList.setType(0);
            sDaoSession.getGoodItemDao().save(dataList);
        }
    }

    public GoodItem queryDataItem(GoodItem dataList) {
        sDaoSession.getGoodItemDao().detachAll();
        return sDaoSession.getGoodItemDao().queryBuilder()
                .where(GoodItemDao.Properties.Djh.eq(dataList.getDjh()),
                        GoodItemDao.Properties.Gysname.eq(dataList.getGysname()),
                        GoodItemDao.Properties.Invcode.eq(dataList.getInvcode()))
                .build().unique();
    }

    public GoodItem queryDataItemPch(GoodItem dataList) {
        sDaoSession.getGoodItemDao().detachAll();
        return sDaoSession.getGoodItemDao().queryBuilder()
                .where(GoodItemDao.Properties.Djh.eq(dataList.getDjh()),
                        GoodItemDao.Properties.Gyspch.eq(dataList.getGyspch()),
                        GoodItemDao.Properties.Gysname.eq(dataList.getGysname()),
                        GoodItemDao.Properties.Invcode.eq(dataList.getInvcode()))
                .build().unique();
    }

    public GoodItem queryById(String djh, Long id) {
        sDaoSession.getGoodItemDao().detachAll();
        return sDaoSession.getGoodItemDao().queryBuilder()
                .where(GoodItemDao.Properties.Djh.eq(djh),
                        GoodItemDao.Properties.Id.eq(id))
                .build().unique();
    }

    // 所有已保存未更新的
    public List<GoodItem> getGoodItemAll(String djh) {
        sDaoSession.getGoodItemDao().detachAll();
        return sDaoSession.getGoodItemDao().queryBuilder()
                .where(GoodItemDao.Properties.Djh.eq(djh),
                        GoodItemDao.Properties.Type.eq("1"))
                .build().list();
    }

    public void deleteAllCache(List<GoodItem> list) {
        sDaoSession.getGoodItemDao().detachAll();
        if (!Utils.isEmpty(list)) {
            for (GoodItem goodItem : list) {
                sDaoSession.getGoodItemDao().delete(goodItem);
                Utils.LogI("deleteAllCache -- " + goodItem.getId());
            }
        }
    }

    public void reset(){
        sDaoSession.getGoodItemDao().deleteAll();
    }

}
